package com.djhu.msg.annotation;

import org.springframework.util.ClassUtils;

import javax.jms.Message;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by zw on 2018/3/16.
 */
public class ObjectProxy implements InvocationHandler {
    private final Object target;

    private final Set<Method> methodset;

    public ObjectProxy(Object target, Set<Method> methods) {
        this.target = target;
        this.methodset = methods;
    }
    public static Object warp(Object obj){
        //Class<?>[] interfaces = obj.getClass().getInterfaces();
        //Method method = ClassUtils.
        Set<Method> methodSet = new HashSet<>();
        Method[] methods = findMethods(obj.getClass());
        for (Method method:methods){
            //BeanUtils.
            if (method.isAnnotationPresent(Listener.class) &&  method.getParameterTypes().length == 1){
                Class<?>[] classes = method.getParameterTypes();
                //method.
                Set<Class<?>> set = ClassUtils.getAllInterfacesForClassAsSet(classes[0]);
                if (set.contains(Message.class)){
                    //Method m = method;
                    methodSet.add(method);
                }
            }
            method.isAnnotationPresent(Listener.class);
        }
        return new ObjectProxy(obj,methodSet);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(proxy,args);
        //return null;
    }




    public static Method[] findMethods(Object object) {
        return object.getClass().getMethods();
    }
}